#!/usr/bin/env/ python
# -*- coding: utf-8 -*-

import os,cv2

def checkout(imgPath,imgSize):
    img = cv2.imread(imgPath)
    img = cv2.resize(img,(imgSize,imgSize))
    with open("loc.txt","r") as f:
        lines = f.readlines()
        for line in lines:
            # classID,labelName,score,xMin,yMin,xMax,yMax,= line.rstrip().split(", ")
            # xMin, yMin, xMax, yMax, score, classID = [i.split("=")[-1] for i in [xMin,yMin,xMax,yMax,score,classID]]

            xMin, yMin, xMax, yMax, score, classID = line.rstrip().split(",")
            xMin, yMin, xMax, yMax, score, classID = [i.split("=")[-1] for i in [xMin,yMin,xMax,yMax,score,classID]]
            xMin, yMin, xMax, yMax = map(int,map(float ,[xMin, yMin, xMax, yMax]))

            for items in line.rstrip().split(", "):
                loc = items.split("=")
                if("xMin"==loc[0]):
                    xMin = int(loc[1])
                if("yMin"==loc[0]):
                    yMin = int(loc[1])
                if ("xMax" == loc[0]):
                    xMax = int(loc[1])
                if ("yMax" == loc[0]):
                    yMax = int(loc[1])
                if ("score" == loc[0]):
                    score = int(loc[1].replace("%",""))
                if ("classID" == loc[0]):
                    classID = int(loc[1])

            x,y,w,h = xMin,yMin,xMax-xMin,yMax-yMin
            print("x,y,w,h:",x,y,w,h)
            print(x,y,w,h)

            if(w>0 and h>0):
                print("useful:",x,y,w,h)
                cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),1,1)
                cv2.putText(img,"score:"+str(score)+", ID:"+str(classID),(x,y),1,1,(0,0,255),1)

    name = imgPath.split("/")[-1].split(".")[0].replace("416","")
    cv2.imwrite("result.jpg",img)
    img  = cv2.resize(img,(960,540))
    cv2.imwrite("data/"+name+".jpg",img)
    cv2.imshow("result",img)
    cv2.waitKey()

def main():

    # imgPath = "/mnt/data/3519/caffeCvtTools/nnie_bgr/data/person416.jpg"
    imgPath = "/mnt/data/3519/demo/mpp/sample/3559/sample_src/mpp/sample/svp/smoke_fight_phone/ins_nnie/data/nnie_image/test/fight_1416.jpg"
    checkout(imgPath,416)

if __name__ == "__main__":
    main()


